<?php
namespace Home\Controller;
use Think\Controller;

class DealController extends  BaseController
{
    public function __construct()
    {
        parent::__construct();

    }
    public function index(){

        $root = get_baseroot();

        $id = intval($_REQUEST['id']);
        \es_session::set("deal_id",$id);
        $user = \es_session::get("user_info");

        //检查其他设备是否已经登录,去除微信
        $is_wechat = \es_session::get("is_wechat");
        if ($is_wechat !== 1) {
            $token = $GLOBALS['user_info']["token"];
            $u_token = \es_cookie::get('token');
            if ($token !== $u_token) {
                //es_session::set('user_info', '');
                //$GLOBALS['user_info'] = '';
                //es_cookie::delete("user_name");
                //es_cookie::delete("user_pwd");
            }
        }

        $root['session_id'] = \es_session::id();
        $user_id  = intval($user['id']);
        if ($user_id >0){

            $root['is_faved'] = $GLOBALS['db']->getOne("SELECT count(*) FROM ".DB_PREFIX."deal_collect WHERE deal_id = ".$id." AND user_id=".$user_id);
        }else{
            $root['is_faved'] = 0;//0：未关注;>0:已关注
        }
        $root['response_code'] = 1;
        $deal = get_deal($id);
        //format_deal_item($deal,$email,$pwd);
        //print_r($deal);
        //exit;
        $root['deal'] = $deal;
        $root['open_ips'] = intval(app_conf("OPEN_IPS"));
        $root['ips_acct_no'] = $user['ips_acct_no'];
        $root['ips_bill_no'] = $deal['ips_bill_no'];

//		function bid_calculate(){
//			//require_once APP_ROOT_PATH."app/Lib/deal_func.php";
//			echo bid_calculate($_POST);
//		}

        $root['ecv_list'] = array();

        if($deal['use_ecv'] == 1){
            //红包抵用
            $user_id = intval($GLOBALS['user_info']['id']);
            $sql = "select e.*,et.name from ".DB_PREFIX."ecv as e left join ".DB_PREFIX."ecv_type as et on e.ecv_type_id = et.id where e.user_id = ".$user_id." AND if(e.use_limit > 0 ,(e.use_limit - e.use_count) > 0,1=1) AND if(e.begin_time >0 , e.begin_time < ".TIME_UTC.",1=1) AND if(e.end_time>0,(e.end_time + 24*3600 - 1) > ".TIME_UTC.",1=1) AND et.use_type !=1  order by e.id desc ";
            //$sql = "select e.*,et.name from ".DB_PREFIX."ecv as e left join ".DB_PREFIX."ecv_type as et on e.ecv_type_id = et.id where e.user_id = ".$user_id." AND if(e.use_limit > 0 ,(e.use_limit - e.use_count) > 0,1=1)   AND et.use_type !=1  order by e.id desc ";
            $root['ecv_list'] = $GLOBALS['db']->getAll($sql);

        }

        if($deal['use_interestrate'] == 1){
            //加息券抵用
            $user_id = intval($GLOBALS['user_info']['id']);
            $sql = "select e.*,et.name from ".DB_PREFIX."interestrate as e left join ".DB_PREFIX."interestrate_type as et on e.ecv_type_id = et.id where ((e.user_id = ".$user_id." and e.to_user_id = 0) or e.to_user_id = ".$user_id.") AND if(e.use_limit > 0 ,(e.use_limit - e.use_count) > 0,1=1) AND if(e.begin_time >0 , e.begin_time < ".TIME_UTC.",1=1) AND if(e.end_time>0,(e.end_time + 24*3600 - 1) > ".TIME_UTC.",1=1) and (et.use_type=1 or et.use_type=2) order by e.id desc ";
            $list = $GLOBALS['db']->getAll($sql);
            foreach($list as $k => $v)
            {
                $list[$k]["rate_format"] = number_format($v["rate"],2)."%";
            }

            $root['interestrate_list'] = $list;

        }

        if (!empty($root['ips_bill_no'])){
            //第三方托管标

            if (!empty($user['ips_acct_no'])){
                $result = GetIpsUserMoney($user_id,0);

                $root['user_money'] = $result['pBalance'];
            }else{
                $root['user_money'] = 0;
            }
        }else{
            $root['user_money'] = $user['money'];
        }

        if($deal['uloadtype'] == 1){
            $root['has_bid_money'] = $GLOBALS['db']->getOne("SELECT sum(money) FROM ".DB_PREFIX."deal_load WHERE deal_id=".$id);
            $root["has_bid_portion"] = intval($has_bid_money)/($deal['borrow_amount']/$deal['portion']);
        }

        $root['user_money_format'] = format_price($user['user_money']);//用户金额

        //data.deal.name
        $root['program_title'] = "投标详情";
        //output($root);
        $root['act'] = CONTROLLER_NAME;
        $root['is_login'] = $this->check_is_login() ;
        $root['hide_back'] = 1 ;
        $this->assign('data',$root);
        $this->display();
    }
//AES_ENCRYPT('18301519608','__FANWEP2P__')
    function test(){
        $user_data["mobile_encrypt"] = "AES_ENCRYPT('18301519608','__FANWEP2P__')";
        if($user_data["mobile_encrypt"] || $user_data["idno_encrypt"] || $user_data["email_encrypt"]){
            $sets = array();
            foreach($user_data as $k => $value){
                $sets[] = "`".$k."`" . " = " .$value;
            }
            if (!empty ($sets)) {
                $sql = 'UPDATE ' . DB_PREFIX."user  SET " . implode(', ', $sets) . ' WHERE id=19' ;
                $rs = $GLOBALS['db']->query($sql);
                echo 1;var_dump($rs);
            }
            echo 2;
        }
        $user=get_user_has('mobile_encrypt',$user_data['mobile_encrypt'],1);
        echo 3;
    }
    function test2(){
        $user_id = 22;

        $user_data_mobile["email_encrypt"] = "AES_ENCRYPT('yzt".$user_id."@163.com','".AES_DECRYPT_KEY."')";
        //$user_data_mobile["email"] = 'yzt'.$user_id."@163.com";
       // $user_data_mobile["wx_openid"] = "oo94b02-QGTBhEaiwP5iBsx28a3w";
        $user_data_mobile["mobile_encrypt"] = "AES_ENCRYPT('".strim(13382888811)."','".AES_DECRYPT_KEY."')";

        if($user_data_mobile["mobile_encrypt"] || $user_data_mobile["wx_openid"] || $user_data_mobile["mobile_encrypt"]){
            $sets = array();
            foreach($user_data_mobile as $k => $value){
                $sets[] = "`".$k."`" . " = " .$value;
            }
            if (!empty ($sets)) {
                $sql = 'UPDATE ' . DB_PREFIX."user  SET " . implode(', ', $sets) . ' WHERE id='.$user_id;
                $GLOBALS['db']->query($sql);
            }
        }
        echo $sql ;
    }
}
?>
